import 'package:enjoy_plus_bryant/pages/house/house_item.dart';
import 'package:enjoy_plus_bryant/utils/eventbus.dart';
import 'package:enjoy_plus_bryant/utils/http.dart';
import 'package:enjoy_plus_bryant/utils/toast.dart';
import 'package:flutter/material.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  // 1. 接收请求的房屋数据
  List<dynamic> houseList = [];
  // 2. 控制等待效果
  bool isLoading = false;

  // 2. 发送请求获取房屋数据
  getHouseList() async {
    try {
      setState(() {
        isLoading = true;
      });
      var res = await http.get('/room');
      // print(res['data']);
      if (res['code'] != 10000) {
        ToastUtil.showError('获取房屋列表失败');
        return;
      }
      ToastUtil.showSuccess('获取房屋列表成功');
      setState(() {
        houseList = res['data'];
      });
      setState(() {
        isLoading = false;
      });
    } catch (e) {
      ToastUtil.showError('获取房屋列表失败');
    }
  }

  @override
  void initState() {
    super.initState();
    getHouseList();
    // 监听登录成功后触发的获取房屋列表事件
    eventBus.on<RefreshHouseListPageEvent>().listen((event) {
      getHouseList();
    });

    // 监听删除房屋后触发事件
    eventBus.on<RemoveRefresh>().listen((event) {
      getHouseList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 241, 238, 238),
      // 导航条
      appBar: AppBar(
        title: const Text('我的房屋'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: isLoading
          ? Center(child: CircularProgressIndicator())
          : Stack(
              children: [
                Padding(
                  padding: const EdgeInsets.only(
                    bottom: 80,
                    left: 10,
                    right: 10,
                  ),
                  child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      return HouseItem(item: houseList[index]);
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Container(
                        height: 10,
                        color: const Color.fromARGB(255, 241, 238, 238),
                      );
                    },
                    itemCount: houseList.length,
                  ),
                ),
                // Positioned 是 Flutter 框架中的一个组件，主要用于在堆叠布局中精确定位和调整子组件的大小。
                // 依赖父组件：必须作为 Stack 组件的直接子组件使用
                Positioned(
                  bottom: 10,
                  left: 0,
                  right: 0,
                  child: Container(
                    padding: const EdgeInsets.all(10),
                    color: Colors.transparent,
                    child: Row(
                      children: [
                        Expanded(
                          child: ElevatedButton(
                            onPressed: () {
                              Navigator.pushNamed(context, '/add_house');
                            },
                            child: const Column(
                              children: [
                                SizedBox(height: 5),
                                Icon(Icons.add),
                                Text('添加房屋'),
                                SizedBox(height: 5),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
    );
  }
}
